%load alt_calib/eq_mit_mass_shock.mat
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../../calibration_v3.5/kimball/eq_mit_mass.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass

set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

Ns = 1 - [7.13]/100;

adj = sum(eq_mit.mu,1);
addpath ../
scx = pick_scale(adj, Ns);

T = 15;

figure(47)
 
 Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);

 % 
 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);

 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;
% 
 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);
% 
 mu_cw = num./tc;

Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all

Y = log_linear_scale_plot(eq_mit.C, scx);

W = log_linear_scale_plot(eq_mit.W, scx);

L = plot(log_linear_scale_plot(total_labor, scx), 'LineWidth', 4);


ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;

mu = log_linear_scale(1./labor_share, scx)

tfp = Z;%eq_mit.C'./total_labor;
A = log_linear_scale(tfp, scx)/tfp(end);

%% Counterfactuals

bl = three_equation_model(A, mu, .5);
fixed_mu = three_equation_model(A, mu(end), .5);
fixed_tfp = three_equation_model(A(end), mu, .5);

% subplot(2,1,1)
% plot(bl.Y, 'LineWidth', 4)
% hold on
% plot(fixed_mu.Y, '--', 'LineWidth', 4)
% plot(fixed_tfp.Y, '-.', 'LineWidth', 4)
% legend('Baseline', 'TFP effect', 'Markup effect', 'Location', 'SouthEast')
% xlim([1 T])
% title('Y')
% 
% subplot(2,1,2)
plot([0:299], 100*(bl.L-1), 'LineWidth', 4)
hold on
plot([0:299],100*(fixed_mu.L-1), '--', 'LineWidth', 4)
plot([0:299],100*(fixed_tfp.L-1), '-.', 'LineWidth', 4)
xlim([0 T-1])
ylim(100*([.975 1.001]-1))
title('L')
legend('Baseline', 'TFP effect', 'Markup effect', 'Location', 'SouthEast')
ytickformat('percentage')

% subplot(2,2,3)
% plot(bl.W, 'LineWidth', 4)
% hold on
% plot(fixed_mu.W, '--', 'LineWidth', 4)
% plot(fixed_tfp.W, '-.', 'LineWidth', 4)
% xlim([1 T])
% title('W')
% 
% subplot(2,2,4)
% plot(A/A(end), 'LineWidth', 4)
% hold on
% plot(mu/mu(end), '--', 'LineWidth', 4)
% legend('TFP','Markup')
% xlim([1 T])

set(gcf,'units','points','position',[10,10,500,300])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'entry_shock_decomp.png')